package cn.fl.product.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.product.service.ISplUsrRService;
import cn.fl.product.transform.SplUsrRVOTransform;
import cn.fl.product.vo.SplUsrRVO;
import com.alibaba.fastjson.JSONObject;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.framework.enums.SplUsrRLogActionCdEnum;
import fl.spl.facade.framework.enums.SplUsrRLogBizSrcCdEnum;
import fl.spl.facade.framework.enums.SplUsrRLogOpTypEnum;
import fl.spl.facade.spl.api.ISplUsrRApiService;
import fl.spl.facade.spl.api.ISplUsrRLogApiService;
import fl.spl.facade.spl.condition.SplUsrRLogQC;
import fl.spl.facade.spl.condition.SplUsrRQC;
import fl.spl.facade.spl.dto.SplUsrRDTO;
import fl.spl.facade.spl.dto.SplUsrRLogDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 供应商-系统用户关系表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class SplUsrRServiceImpl implements ISplUsrRService {

	/**
	 * 供应商-系统用户关系表 暴露服务
	 */
	@Autowired
	private ISplUsrRApiService splUsrRApiService;
	/**
	 * 数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public SplUsrRVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			throw new BaseException(BaseExceptionConstant.COMMON_0001, BaseExceptionConstant.COMMON_0001_NAME);
		}
		SplUsrRDTO dto = this.splUsrRApiService.selectSplUsrRById(id);
		return SplUsrRVOTransform.toVO(dto);
	}

	@Override
	public Long add(SplUsrRVO entity) throws BaseException{
		if(null == entity){
			throw new BaseException(BaseExceptionConstant.COMMON_0001, BaseExceptionConstant.COMMON_0001_NAME);
		}
		return this.splUsrRApiService.addSplUsrRByAdmin(SplUsrRVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException{
		if(CollectionUtils.isEmpty(ids)){
			throw new BaseException(BaseExceptionConstant.COMMON_0001, BaseExceptionConstant.COMMON_0001_NAME);
		}
		return this.splUsrRApiService.removeSplUsrRByIds(ids,CurrentThreadContext.getCurrentUserId());
	}

	@Override
	public boolean modify(SplUsrRVO entity) throws BaseException{
		if(null == entity){
			throw new BaseException(BaseExceptionConstant.COMMON_0001, BaseExceptionConstant.COMMON_0001_NAME);
		}
		SplUsrRDTO dto = SplUsrRVOTransform.toDTO(entity);
		return this.splUsrRApiService.modifySplUsrRByAdmin(dto);
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		SplUsrRQC condition=null;
		if(null == page) {
			page = new Pager();
			page.setCondition(new SplUsrRQC());
		}
		condition=(SplUsrRQC)page.getCondition();
//        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
//        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
//        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		page=this.splUsrRApiService.searchSplUsrRListPage(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())){
			List<SplUsrRVO> voList=SplUsrRVOTransform.toVOList((List<SplUsrRDTO>)page.getResultList());
			this.setDictData(voList);
			page.setResultList(voList);
		}
		return page;
	}

	@Override
	public List<SplUsrRVO> searchList(SplUsrRQC condition) throws BaseException{
		if(null == condition){
			condition=new SplUsrRQC();
		}
//		condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
//		condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
//		condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		List<SplUsrRVO> voList=SplUsrRVOTransform.toVOList(this.splUsrRApiService.searchSplUsrRList(condition));
		this.setDictData(voList);
		return voList;
	}

	/**
	 * 添加数据字典名称
	 * @param voList
	 */
	public void setDictData(List<SplUsrRVO> voList){
		if(CollectionUtils.isEmpty(voList)){
			return;
		}
		Map<String, String> _CdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), DictionaryConstant.SPL_BIZ_TYP_CD_GRP_CD);
		Map<String, String> isPrivateMap = this.sysDictionaryApiService.searchGroupToMap(SplSystemConstant.SPL_SYS_CD, SplSystemConstant.DEFAULT_ORG_CD,fl.spl.facade.framework.constant.DictionaryConstant.SPL_IS_NOT_PRIVATE_SEA_CD);
		for(SplUsrRVO vo:voList){
			if(CollectionUtils.isMapNotEmpty(_CdMap) && StringUtils.isNotEmpty(vo.getSplBizTypCd())){
				String typNm = "";
				String[] cd = vo.getSplBizTypCd().split(";");
				for (String str : cd) {
					typNm = typNm + ";"+_CdMap.get(str);
				}
				vo.setSplBizTypCdNm(typNm.substring(1));
			}
			if(CollectionUtils.isMapNotEmpty(isPrivateMap) && isPrivateMap.containsKey(vo.getIsPrivate())){
				vo.setIsPrivateCdNm(isPrivateMap.get(vo.getIsPrivate()));
			}
		}
	}

	/**
	 * 查询数据字典
	 * @param groupCd 组代码
	 * @return List<KeyValueVO>
	 */
	@Override
	public List<KeyValueVO> searchCdList(String groupCd){
		return this.sysDictionaryApiService.getKvList(groupCd, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
	}

	@Override
	public List<KeyValueVO> searchSplBizTypCdList() throws BaseException {
		return this.searchCdList(DictionaryConstant.SPL_BIZ_TYP_CD_GRP_CD);
	}

	@Override
	public boolean removeById(Long id,Long usrId) throws BaseException {
		if(StringUtils.isEmpty(id)){
			throw new BaseException(BaseExceptionConstant.COMMON_0001,"删除数据主键不能为空！");
		}
		return this.splUsrRApiService.removeSplUsrRById(id,usrId);
	}
}